{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.finfo使用方法\n",
    "\n",
    "eps是一个很小的非负数\n",
    "\n",
    "除法的分母不能为0的,不然会直接跳出显示错误。\n",
    "\n",
    "使用eps将可能出现的零用eps来替换，这样不会报错。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "def gaussian2D(shape, sigma=1):\n",
    "    m, n = [(ss - 1.) / 2. for ss in shape]\n",
    "    y, x = np.ogrid[-m:m+1,-n:n+1]\n",
    "\n",
    "    \n",
    "    h = np.exp(-(x * x + y * y) / (2 * sigma * sigma))\n",
    "    h[h < np.finfo(h.dtype).eps * h.max()] = 0\n",
    "    return h"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如何利用二维数组实现矩阵的转置？\n",
    "https://www.zhihu.com/question/567714290"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.random.rand(2,2)\n",
    "print(a)\n",
    "\n",
    "b = a.transpose()\n",
    "print(b)\n",
    "\n",
    "#1024*768的图像，w*h*c，转换成c*h*w，即为pytorch里的图像tensor数据格式 \n",
    "h=768\n",
    "w=1024\n",
    "c = np.random.randint(0,high=255, size=(w,h,3))\n",
    "print(c.shape)\n",
    "\n",
    "d = c.transpose(2,1,0)\n",
    "print(d.shape)\n",
    "\n",
    "e = np.random.rand(3,2)\n",
    "print(e.shape)\n",
    "\n",
    "f = np.zeros((2,3), np.float64)\n",
    "for i in range(3):\n",
    "    for j in range(2):\n",
    "        print(e[i,j])\n",
    "        f[j,i] = e[i,j]\n",
    "\n",
    "print(e,f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['感' '谢' '热']\n",
      " ['心' '人' '解']\n",
      " ['答' '问' '题']]\n",
      "[['人' '解']\n",
      " ['问' '题']]\n",
      "Found at location 1 1\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "shape mismatch: objects cannot be broadcast to a single shape",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32md:\\work\\code\\kindlytree_aics\\python\\numpy.ipynb Cell 5\u001b[0m in \u001b[0;36m<cell line: 28>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     <a href='vscode-notebook-cell:/d%3A/work/code/kindlytree_aics/python/numpy.ipynb#W4sZmlsZQ%3D%3D?line=23'>24</a>\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m Found:\n\u001b[0;32m     <a href='vscode-notebook-cell:/d%3A/work/code/kindlytree_aics/python/numpy.ipynb#W4sZmlsZQ%3D%3D?line=24'>25</a>\u001b[0m     \u001b[39mprint\u001b[39m(\u001b[39m'\u001b[39m\u001b[39mNot found\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m-\u001b[39m\u001b[39m1\u001b[39m, \u001b[39m-\u001b[39m\u001b[39m1\u001b[39m)\n\u001b[1;32m---> <a href='vscode-notebook-cell:/d%3A/work/code/kindlytree_aics/python/numpy.ipynb#W4sZmlsZQ%3D%3D?line=27'>28</a>\u001b[0m result \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39mwhere(n \u001b[39m==\u001b[39;49m m)\n\u001b[0;32m     <a href='vscode-notebook-cell:/d%3A/work/code/kindlytree_aics/python/numpy.ipynb#W4sZmlsZQ%3D%3D?line=28'>29</a>\u001b[0m \u001b[39mprint\u001b[39m(result)\n",
      "\u001b[1;31mValueError\u001b[0m: shape mismatch: objects cannot be broadcast to a single shape"
     ]
    }
   ],
   "source": [
    "#Python numpy 能不能在一个大矩阵中查找一个小矩阵？\n",
    "import numpy as np\n",
    "\n",
    "ls = list( dict.fromkeys( '感谢热心人解答问题' ,None) )\n",
    "\n",
    "n = np.array( ls ).reshape( 3 , 3)\n",
    "\n",
    "print( n )\n",
    "\n",
    "sub_ls = list( dict.fromkeys( '人解问题' ,None) )\n",
    "m = np.array( sub_ls ).reshape( 2 , 2)\n",
    "print(m)\n",
    "\n",
    "a, b = n.shape[0], n.shape[1]\n",
    "c, d = m.shape[0], m.shape[1]\n",
    "Found = False\n",
    "for start_row in range(0, a-c+1):\n",
    "    for start_col in range(0, b-d+1):\n",
    "        e = n[start_row:start_row+c,start_col:start_col+d] == m\n",
    "        #print(m, e)\n",
    "        if e.all():\n",
    "            Found= True\n",
    "            print('Found at location', start_row, start_col)\n",
    "if not Found:\n",
    "    print('Not found', -1, -1)\n",
    "\n",
    "#result = np.where(n == m)\n",
    "#print(result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "ad2bdc8ecc057115af97d19610ffacc2b4e99fae6737bb82f5d7fb13d2f2c186"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
